package com.woniuxy.threeproject.end_water_project_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.threeproject.end_water_project_user.dao.UserMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Repair;
import com.woniuxy.threeproject.end_water_project_user.dao.RepairMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.User;
import com.woniuxy.threeproject.end_water_project_user.domain.form.repairform.RepairForm;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterProjectUserException;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterRuntimeException;
import com.woniuxy.threeproject.end_water_project_user.service.IRepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 维修表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
@Slf4j
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements IRepairService {
    @Resource
    private RepairMapper repairMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public Boolean doRepair(RepairForm repairForm) {
        log.info("开始执行新增维修业务,{}",repairForm);
        //通过姓名查询用户信息并存入Repair
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",repairForm.getPhone());
        List<User> users = userMapper.selectList(queryWrapper);
        if(users.size()==0){
            throw new EndWaterRuntimeException("不存在此用户");
        }
        Repair repair = new Repair();
        repair.setUserId(users.get(0).getUserId());
        repair.setWaterMeterId(users.get(0).getWaterMeterId());
        repair.setReason(repairForm.getReason());
        //设置维修员工id（此处默认设置为1，实际应该在后台管理中添加）
//        repair.setEmployeeId(1);
        //设置维修时间（此处默认设置为现在时间，实际应该在后台管理中添加）
//        repair.setRepairTime(new Date());
        //设置提交时间
        repair.setSubmitTime(new Date());
        log.info(repair.toString());
        int insert = repairMapper.insert(repair);
        //若修改原因不为空，则修改数据库的值
        if(insert>0){
            return true;
        }
        return false;
    }
}
